//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.Research.DataLayer
{
    public class OPCConnectionParameters : ConnectionParametersBase
    {
        public static IConnMgrInfoParams CreateConnection(string name, string path)
        {
            OPCConnectionParameters ret = new OPCConnectionParameters(Guid.NewGuid(), name, path);
            return ret;
        }

        private OPCConnectionParameters(Guid id, string name, string path)
            : base(id, name)
        {
            Update(name, path);
        }

        private static OPCConnectionParameters FromXml(datasource.DataSource dataSource)
        {
            OPCConnectionParameters ret = new OPCConnectionParameters(
                new Guid(dataSource.id),
                dataSource.name,
                dataSource.GetPropertyAsString("path"));
            return ret;
        }

        private datasource.DataSource ToXml()
        {
            datasource.DataSource ret = new datasource.DataSource();
            ret.id = this.ID.ToString();
            ret.name = this.Name;
            ret.SetPropertyString("path", (string)Parameters["path"]);
            return ret;
        }

        internal void Load(out string path)
        {
            path = (string)Parameters["path"];
        }

        internal void Update(string name, string path)
        {
            Name = name;
            Parameters["path"] = path;
        }

        #region Connection Parameters Helpers

        public class Helper : IConnMgrInfoParamsHelper
        {
            public IConnMgrInfoParams FromXml(datasource.DataSource dataSource)
            {
                return OPCConnectionParameters.FromXml(dataSource);
            }

            public datasource.DataSource ToXml(IConnMgrInfoParams preset)
            {
                return ((OPCConnectionParameters)preset).ToXml();
            }

            public bool HandlesConnection(IConnMgrInfoParams conn)
            {
                return (conn is OPCConnectionParameters);
            }

            public Connection OpenConnection(IConnMgrInfoParams preset)
            {
                OPCConnectionParameters connInfo = (OPCConnectionParameters)preset;
                Connection conn = OPCConnection.Create(connInfo);
                conn.Open();
                return conn;
            }

            public bool TestConnection(IConnMgrInfoParams preset, out BackendStorageException error)
            {
                OPCConnectionParameters connInfo = (OPCConnectionParameters)preset;
                Connection conn = OPCConnection.Create(connInfo);
                return conn.Test(out error);
            }
        }

        #endregion
    }
}
